import re
import string

import model
import webtool

class ConcreteAdapter(model.Adapter):
    
    postListUrlTheme = r'http://bbs.nju.edu.cn/bbstdoc?board=%s'
    postListUrl = r'http://bbs.nju.edu.cn/bbsdoc?board=%s&type=tdoc'
    boardname = 'SecondMarket'
        
    def __init__(self):
        model.Adapter.__init__(self)
        
    
    def setBoardName(self,name):
        self.boardname = name
    
    def getName(self):
        return u'Lily BBS'
    
    def getPostList(self, thememode):
        if thememode:
            return self.getThemeModePostList()
        else:
            return self.getNormalModePostList()
    
    def getNormalModePostList(self):
        list = []
        content = webtool.getPageText(self.getPageUrl(False))
        ls = re.findall(r'<tr><td>.*?</font>',content)
        for l in ls:
            p = self.parsePost(l, False)
            if p:
                list.append(p)
        return list
    
    def getThemeModePostList(self):
        list = []
        content = webtool.getPageText(self.getPageUrl(True))
        ls = re.findall(r'<tr><td>.*?</font>',content)
        for l in ls:
            p = self.parsePost(l, True)
            if p:
                list.append(p)
        return list
    
    def getPostDetail(self, post):
        content = webtool.getPageText(post.getLink())
        if content:
            content = content.replace('\n','\\n')
            content = content.replace('\r','')

            ls = re.findall(r'\)\\n.*\\n--\\n',content , re.MULTILINE)
            if len(ls):
                item = ls[0]
                item =item.replace('\\n','\n')
                item = item[2:-4]
                post.postContent = item
                
    def getPageUrl(self, thememode):
        if thememode:
            return self.postListUrlTheme % (self.boardname,)
        else:
            return self.postListUrl % (self.boardname,)
    
    
    def setdata(self, content, postlist):
        ls = re.findall(r'(docWriter\(.*\)\;)', content, re.M)
        if len(ls):
            str = ls[0]
            pattern = r"docWriter\('.*?',(?P<bid>[0-9]+),.*\);"
            m = re.match(pattern , str)
            if m and m.group('bid'):
                for p in postlist:
                    p.setData('bid', m.group('bid'))
                        
    def parsePost(self,data, thememode):
        line =  unicode(data)
        if thememode:
            pattern = r".*>(?P<id>\d+).*userid=(?P<userid>[0-9a-zA-Z]+).*<td>(?P<time>[^<]+).*href=(?P<link>[^>]+)>(?P<title>[^<]+)</a>.*"
        else:
            pattern = r".*>(?P<id>\d+).*userid=(?P<userid>[0-9a-zA-Z]+).*<nobr>(?P<time>[^<]+).*href=(?P<link>[^>]+)>(?P<title>[^<]+)</a>.*"
        m = re.match(pattern, line)
        if not m:
            return None
        postID = m.group('id')
        postUser = m.group('userid')
        postTime = parsetime(m.group('time'))
        postTitle = m.group('title')
        postLink = u'http://bbs.nju.edu.cn/'+m.group('link')
        p= model.Post(title= postTitle, time=postTime, id = postID, user=postUser,link = postLink)
        return p
    
    def getCopy(self):
        newAdapter = ConcreteAdapter()
        newAdapter.boardname = self.boardname
        return newAdapter
    

import time
def parsetime(t):
    t = "09 %s"%(t,)
    return time.strptime(t, "%y %b %d %H:%M")

if __name__=='__main__':
    adapter = ConcreteAdapter()
    adapter.setBoardName('NJUExpress')
    list = adapter.getPostList(False)
    for l in list:
        adapter.getPostDetail(l)